package com.study.controller;

import com.study.entity.User;
import com.study.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * Created by guanxy on 2017/10/18.
 */
@Controller
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);  //打印日志信息Logger方法

    @Autowired
    private IUserService userService;   //注入IUserService

    @PostMapping("doLogin")
    public String doLogin(User user, HttpSession session, RedirectAttributes ra){
        User u = userService.findUser(user);    //通过页面传来的用户名密码调用IUserService的方法查找用户对象
        logger.debug("当前登录用户={}",u);    //打印用户信息
        if(u==null){    //当登录用户不存在时
            ra.addAttribute("error","用户名密码错误");     //打印错误信息
            session.setAttribute("userName",user.getUserName());
            session.setAttribute("userPwd",user.getUserPwd());
            return "redirect:login.jsp";    //重定向到登录界面
        }else{
            session.setAttribute("user",u);     //将用户信息保存到session
            return "main";      //跳转到main页面
        }
    }

}
